package com.rlyy.rsj.service;

import com.rlyy.aibase.utils.base.TableNode;
import com.rlyy.rsj.dao.OrgpDao;
import com.rlyy.rsj.utils.RedisUtils;
import org.springframework.util.CollectionUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.rlyy.rsj.utils.RedisUtils.SEVENDAYS;
import static com.rlyy.rsj.utils.RedisUtils.ThreeMinutes;


public class OrgpService {
private static final OrgpService instance= new OrgpService();
	
	public static OrgpService getInstance(){
		return instance;
	}
	private static OrgpDao infoDao = OrgpDao.getInstance();

	public void updateForTimmer(Connection conn)throws SQLException{
		
		infoDao.updateOrgpStatus(conn);
		infoDao.updateProdStatus(conn);
		
		TableNode tn = infoDao.findSysStatus(conn);
		List<Map<String,Object>> list = tn.toList();
		for(Map<String,Object> map : list){
			String key = "rsj|prodStatus|"+map.get("Aut_Keyid")+"_"+map.get("Prod_Uid");

			HashMap<String,String> redisMap = new HashMap<>();
			redisMap.put("Sta_Orgp_Id", (String) map.get("Sta_Orgp_Id"));
			redisMap.put("Sta_Status", (String) map.get("Sta_Status"));
			redisMap.put("Sta_Charge_Mode", (String) map.get("Sta_Charge_Mode")); //计费方式

			RedisUtils.hmset(key,redisMap,SEVENDAYS); //由3min -> 7d
		}
		//清除所有停用合同的key
		TableNode orgpNotInSysStatus = infoDao.findOrgpNotInSysStatus(conn);
		for(Map<String,Object> notMap : orgpNotInSysStatus.toList()){
			String key = "rsj|prodStatus|"+notMap.get("Aut_Keyid")+"_"+notMap.get("Prod_Uid");
			RedisUtils.del(key);
		}
	}
}
